package com.fairytask.engine.components
{
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.checkBox.CheckBox;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.comboBox.ComboBox;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.list.List;
	import com.fairytask.engine.components.radioButton.RadioButton;
	import com.fairytask.engine.components.radioButton.RadioButtonGroup;
	import com.fairytask.engine.components.tab.TabBar;
	import com.fairytask.engine.components.table.Table;
	import com.fairytask.engine.components.text.Input;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.ScrollText;
	import com.fairytask.engine.components.text.StaticText;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.components.tree.Tree;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;

	public class ComponentCreater
	{
		private static var _self:ComponentCreater;

		public function ComponentCreater()
		{
			
		}

		public static function getInstance():ComponentCreater
		{
			if (_self == null)
			{
				_self=new ComponentCreater();
			}
			return _self;
		}

		public function CreateControl(type:String, pList:Array, returnObj:Object, returnVarName:String):void
		{
			switch (type)
			{
				case ComponentType.Window:
					returnObj[returnVarName]=CreateWindow(pList[0], uint(pList[1]), uint(pList[2]), pList[3], pList[4]);
					break;
				case ComponentType.CustomControl:
					returnObj[returnVarName]=CreateCustomControl(pList[0]);
					break;
				case ComponentType.SimpleButton:
					returnObj[returnVarName]=CreateSimpleButton(pList[0]);
					break;
				case ComponentType.Sprite:
					returnObj[returnVarName]	=CreateSprite()
					break;
				case ComponentType.Button:
					returnObj[returnVarName]	=CreateButton(pList[0], uint(pList[1]), uint(pList[2]), pList[3],pList[4],pList[5],pList[6],pList[7]);
					break;
				case ComponentType.ViewGroup:
					returnObj[returnVarName]	=CreateViewGroup(uint(pList[0]), uint(pList[1]), pList[2],int(pList[3]),int(pList[4]));
					break;
				case ComponentType.List:
					returnObj[returnVarName]	=CreateList(int(pList[0]), int(pList[1]), pList[2],[pList[3]], pList[4], pList[5]);
					break;
				case ComponentType.TabBar:
					returnObj[returnVarName]	=CreateTabBar(uint(pList[0]), uint(pList[1]), Number(pList[2]), pList[3], 
											pList[4], int(pList[5]), int(pList[6]), Number(pList[7]), Number(pList[8]));
					break;
				case ComponentType.Table:
					returnObj[returnVarName]	=CreateTable(Number(pList[0]), int(pList[1]), pList[2], pList[3], pList[4], pList[5]);
					break;
				case ComponentType.Input:
					returnObj[returnVarName]	=CreateInput(uint(pList[0]), uint(pList[1]), pList[2], pList[3], uint(pList[4]), uint(pList[5]));
					break;
				case ComponentType.ComboBox:
					returnObj[returnVarName]	=CreateComboBox(uint(pList[0]), uint(pList[1]), uint(pList[2]), uint(pList[3]),pList[4],pList[5],pList[6],pList[7]);
					break;
				case ComponentType.Label:
					returnObj[returnVarName]	=CreateLabel(pList);
					break;
				case ComponentType.StaticText:
					returnObj[returnVarName]	=CreateStaticText(pList);
					break;
				case ComponentType.CheckBox:
					returnObj[returnVarName]	=CreateCheckBox(pList[0]);
					break;
				case ComponentType.Text:
					var hasScroll	:Boolean	=	true;
					if(null!=pList[4])
					{
						hasScroll	=	Boolean(int(pList[4]));
					}
					returnObj[returnVarName]	=CreateText(uint(pList[0]), uint(pList[1]), pList[2], pList[3],hasScroll);
					break;
				case ComponentType.Tree:
					returnObj[returnVarName]	=CreateTree(pList[0],pList[1],pList[2],pList[3],pList[4]);
					break;
				case ComponentType.Clip:
					returnObj[returnVarName]	=CreateClip(pList);
					break;
				case ComponentType.RadioButton:
					returnObj[returnVarName]	=CreateRadioButton(pList[0],pList[1],pList[2],pList[3]);
					break;
				case ComponentType.RadionButtonGroup:
					returnObj[returnVarName]	=CreateRadioButtonGroup();
					break;
				case ComponentType.ScrollText:
					returnObj[returnVarName]	=CreateScrollText(int(pList[0]),int(pList[1]),pList[2],pList[3],pList[4],Boolean(int(pList[5])));
					break;
			}
		}
		
		/**
		 * 图文混排 
		 * @param _w
		 * @param _h
		 * @param _vSkin
		 * @param _hStrin
		 * @param _resPath
		 * "res/icon.xml"
		 */
		public function CreateScrollText(_w:int,_h:int,_vSkin:String,_hStrin:String,_resPath:String,_hasScroll:Boolean=true):ScrollText
		{
			return new ScrollText(_w,_h,_vSkin,_hStrin,_resPath,_hasScroll);
		}
		
		/**
		 *滚动条 
		 * @param vSkin
		 * @param hSkin
		 * @return 
		 * 
		 */
		public function CreateScrollSkin(vSkin:String="",hSkin:String=""):ScrollSkin
		{
			var scrollSkin:ScrollSkin;
			if (vSkin!= "" || hSkin!="")
			{
				scrollSkin=new ScrollSkin(vSkin,hSkin);
			}
			return scrollSkin;
		}
		/**
		 * 创建自定义组件
		 * @param _skinClassName
		 * @return
		 */
		public function CreateCustomControl(_skinClassName:String=""):MovieClip
		{
			if (_skinClassName != "" && _skinClassName != null)
			{
				return ResManager.newResClass(_skinClassName) as MovieClip;
			}
			else
			{
				return new MovieClip();
			}
		}
		
		public function CreateSimpleButton(skinName:String=""):SimpleButton
		{
			if (skinName != "" && skinName != null)
			{
				return ResManager.newResClass(skinName) as SimpleButton;
			}
			else
			{
				return new SimpleButton();
			}
		}
		

		/**
		 * 空组件
		 * @return sprite
		 *
		 */
		public function CreateSprite():Sprite
		{
			return new Sprite();
		}

		/**
		 * 创建基本窗口
		 * @param title
		 * @param w
		 * @param h
		 * @param _skinClassName
		 * @param backType
		 * @return
		 *
		 */
		public function CreateWindow(title:String="", w:uint=50, h:uint=20, _skin:String="",_skinBnt:String=""):Window
		{
			var tmpWindow:Window;
			if (_skin == "")
			{
				tmpWindow=new Window(title, w, h, null);
			}
			else
			{
				tmpWindow=new Window(title, w, h, _skin,_skinBnt);
			}
			return tmpWindow;
		}

		/**
		 *创建基本按钮
		 * @param w
		 * @param h
		 * @param label 标签
		 * @param skin 皮肤
		 * @return
		 *
		 */
		public function CreateButton(label:String="",w:uint=30, h:uint=20,  skin:String="",top:int=2,icon:* = "",iconX:int = 0,iconY:int = 0):Button
		{
			var tmpButton:Button;
			if (skin == "")
			{
				tmpButton=new Button(label,w, h,  null,top,icon,iconX,iconY);
			}
			else
			{
				tmpButton=new Button(label,w, h,  skin,top,icon,iconX,iconY);
			}
			return tmpButton;
		}
       public function CreateComboBox(width:uint,height:uint,left:uint,listheight:uint,txtSkinName:String,listSkinName:String,listBackName:String,dataList:Array):ComboBox
	   {
		   var combobox:ComboBox;
		   combobox=new ComboBox(width,height,left,listheight,txtSkinName,listSkinName,listBackName,dataList);
		   return combobox;
	   }
	   /**
	    * 创建Label 只适合不更改的 
	    * @param args
	    * @return 
	    * 
	    */
	   public function CreateLabel(...args):Label
	   {
		   if(args[0].length>0)
		   {
			   return new Label(args[0][0],args[0][1]);
		   }
		   return new Label();
	   }
	   /**
	    * 创建静态文本 
	    * @param args
	    * @return 
	    * 
	    */
	   public function CreateStaticText(...args):StaticText
	   {
		   if(args[0].length>0)
		   {
			   return new StaticText(args[0],args[1]);
		   }
		   return new StaticText();
	   }
		/**
		 *
		 * @param row
		 * @param col
		 * @param baseItemRes
		 * @return
		 *
		 */
		public function CreateViewGroup(row:uint=10, col:uint=10, baseItemRes:String="",pw:int=0,ph:int=0):ViewGroup
		{
			var tmpViewGroup:ViewGroup;
			if (baseItemRes == "")
			{
				tmpViewGroup=new ViewGroup(row, col, null,pw,ph);
			}
			else
			{
				tmpViewGroup=new ViewGroup(row, col, baseItemRes,pw,ph);
			}
			return tmpViewGroup;
		}

		/**
		 *
		 * @param w
		 * @param h
		 * @param itemData
		 * @param backgroundSkin
		 * @param itemSkinStr
		 * @return
		 *
		 */
		public function CreateList(w:int=40, h:int=20, useScroll:Boolean=false,itemData:Array=null, 
								   backgroundSkin:String="", itemSkinStr:String=""):List
		{
			var tmpList:List;
			var listData	:Array	=	[];
			
			if(itemData!=null&&itemData[0].length>0)
			{
				for(var i:int=0;i<itemData[0].length;i++)
				{
					var tmpItem:Object	=	{label:itemData[0][i],height:25,skin:itemSkinStr};
					listData.push(tmpItem);
				}
			}
			
			tmpList=new List(w, h, useScroll,listData, backgroundSkin, itemSkinStr);
			return tmpList;
		}

		/**
		 *
		 * @param itemWidth
		 * @param itemHeight
		 * @param itemSpace
		 * @param itemSkin
		 * @param itemList
		 * @param viewX
		 * @param viewY
		 * @param viewW
		 * @param viewH
		 * @return
		 *
		 */
		public function CreateTabBar(itemWidth:uint=80, itemHeight:uint=25, itemSpace:Number=0, itemSkin:String="", itemList:Array=null, viewX:int=10, viewY:int=56, viewW:Number=150, viewH:Number=100):TabBar
		{
			var tmpTabBar:TabBar;
			if (itemSkin == "")
			{
				tmpTabBar=new TabBar(itemWidth, itemHeight, itemSpace, null, itemList, viewX, viewY, viewW, viewH);
			}
			else
			{
				tmpTabBar=new TabBar(itemWidth, itemHeight, itemSpace, itemSkin, itemList, viewX, viewY, viewW, viewH);
			}
			return tmpTabBar;
		}

		/**
		 *
		 * @param pWidth
		 * @param pRow
		 * @param pTitleData
		 * @param pTitleSkin
		 * @param pItemSkin
		 * @param pPage
		 * @return
		 *
		 */
		public function CreateTable(pWidth:Number=80, pRow:int=25, _titleStrArr:Array=null, _skArr:Array=null, _sizeArr:Array=null, pPage:Boolean=false):Table
		{
			var tmpTable:Table;
			tmpTable=new Table(pWidth, pRow, _titleStrArr, _skArr, _sizeArr, pPage);
			return tmpTable;
		}

		/**
		 *
		 * @param w
		 * @param h
		 * @param _skin
		 * @param l
		 * @param t
		 * @param c
		 * @return
		 *
		 */
		public function CreateInput(w:uint=50, h:uint=20, _skin:String=null, l:uint=0, t:uint=0, c:uint=0xeeff55):Input
		{
			var tmpInput:Input;
			tmpInput=new Input(w, h, _skin, l, t, c);
			return tmpInput;
		}
	/**
	 *
	 * @param w
	 * @param h
	 * @param vScrollSkin
	 * @param hScrollSkin
	 * @return
	 *
	 */
		public function CreateCheckBox(skin:String):CheckBox
		{
			return new CheckBox(skin);
		}
		public function CreateText(w:uint,h:uint,vScrollSkin:String,hScrollSkin:String,hasScroll:Boolean=true):Text
		{
			return new Text(w,h,vScrollSkin,hScrollSkin,hasScroll);
		}
		public function CreateTree(_w:int,_h:int,skin:String,hideRoot:Boolean=false,hideSelect:Boolean=false):Tree
		{
			return new Tree(_w,_h,skin,hideRoot,hideSelect);
		}
		public function CreateClip(pList:Array):Clip
		{
			var tmpClip	:Clip	=	new Clip();
			if(pList.length>0)
			{
				tmpClip.source	=	pList[0];
			}
			return tmpClip;
		}
		/**
		 * 
		 * @param strlab
		 * @param skin
		 * @return 
		 * 
		 */		
		public function CreateRadioButton(strlab:String,skin:String=null,strHover:Boolean=false,overEffect:Boolean=false):RadioButton
		{
			return new RadioButton(strlab,skin,strHover,overEffect);
		}
		public function CreateRadioButtonGroup():RadioButtonGroup
		{
			return new RadioButtonGroup();
		}
	}
}
